package com.wangzhixuan.commons.enums;

import lombok.Getter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;

import java.time.Duration;
import java.util.Objects;

/**
 * 缓存名称枚举
 *
 * @author lixin
 */
@Getter
public enum CacheEnum {
    /**
     * 用户信息缓存名称
     */
    ACTIVE_SESSION_CACHE("activeSessionCache", Duration.ZERO,new JdkSerializationRedisSerializer()),

    /**
     * 授权缓存名称
     */
    AUTHORIZATION_CACHE("authorizationCache", Duration.ZERO,new JdkSerializationRedisSerializer()),

    /**
     * 权限认证缓存名称
     */
    AUTHENTICATION_CACHE("authenticationCache", Duration.ZERO,new JdkSerializationRedisSerializer()),

    /**
     * 半小时缓存名称
     */
    HALF_HOUR("halfHour", Duration.ofMinutes(30),null),

    /**
     * 一天缓存
     */
    ONE_DAY("oneDay", Duration.ofDays(1),null),

    /**
     * 10分钟缓存名称
     */
    TEN_MINUTES("tenMinutes", Duration.ofMillis(10),null),

    /**
     * 1小时缓存名称
     */
    HOUR("hour", Duration.ofHours(1),null),

    /**
     * 验证码缓存名称
     */
    CAPTCHA_CACHE("captchaCache", Duration.ofMinutes(2),null);
    /**
     * 缓存名称
     */
    private String name;
    /**
     * 缓存失效时间，可为null
     */
    private Duration cacheTime;
    /**
     * 缓存value序列化方式
     */
    private RedisSerializer<?> valueSerializer;

    CacheEnum(String name, Duration cacheTime, RedisSerializer<?> valueSerializer) {
        Assert.notNull(name, "cacheName must not be null!");
        this.name = name;
        this.cacheTime = cacheTime;
        this.valueSerializer = Objects.isNull(valueSerializer) ? new Jackson2JsonRedisSerializer<>(Object.class) : valueSerializer;
    }
}
